package com.example.yrdev.ordering_system.Report;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.example.yrdev.ordering_system.Model.MemberCardReportType;
import com.example.yrdev.ordering_system.R;

import java.util.ArrayList;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by luke on 12/30/16.
 */

public class ReportTypeDialog extends Dialog {
    public interface ReportTypeListener{

        void selectedReport(MemberCardReportType memberCardReportType);

    }
    @InjectView(R.id.tit)TextView tit;
     @InjectView(R.id.content_container)ListView contentContainer;
   private ArrayList<MemberCardReportType>memberCardReportTypeArrayList;
    private ReportTypeListener reportTypeListener;

    public void setReportTypeListener(ReportTypeListener reportTypeListener) {
        this.reportTypeListener = reportTypeListener;
    }

    public ReportTypeDialog(Context context){
        super(context);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.card_type_info_dialog);
        ButterKnife.inject(this);

        initView();

    }

    public void initView(){
             tit.setText("请选择报表类型");
             memberCardReportTypeArrayList=MemberCardReportType.getInstance().getMemberCardReportTypeArrayList();
          contentContainer.setAdapter(new CustomAdapter(getContext()));
        contentContainer.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (reportTypeListener!=null)
                    reportTypeListener.selectedReport(memberCardReportTypeArrayList.get(position));



            dismiss();

            }
        });
    }


    private class CustomAdapter extends BaseAdapter {
        LayoutInflater layoutInflater;

        CustomAdapter(Context context){

            layoutInflater=LayoutInflater.from(context);

        }
        @Override
        public int getCount() {
            return memberCardReportTypeArrayList.size();

        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {

            if (view==null){

                view=    layoutInflater.inflate(R.layout.card_info_type_row,null);

            }

            MemberCardReportType info=memberCardReportTypeArrayList.get(i);

            TextView textView=(TextView)view.findViewById(R.id.cardTypeInfo);
            textView.setText(info.getReportName());


            return view;

        }
    }
}
